import 'package:get/get.dart';

class DioResponse<T> {
  /// 消息(例如成功消息文字/错误消息文字)
  final String? errmsg;

  /// 自定义code(可根据内部定义方式)
  final int? errcode;

  /// 接口返回的数据
  final T? data;

  /// 需要添加更多
  /// .........

  DioResponse({
    this.errmsg,
    this.data,
    this.errcode,
  });

  @override
  String toString() {
    StringBuffer sb = StringBuffer('{');
    sb.write("\"errmsg\":\"$errmsg\"");
    sb.write(",\"errorMsg\":\"$errcode\"");
    sb.write(",\"data\":\"$data\"");
    sb.write('}');
    return sb.toString();
  }
}

enum DioResponseCode {
  /// 成功
  SUCCESS(200),

  /// 错误
  ERROR(500),

  ///501 未登录
  NOT_LOGIN(501),

  ///502 参数校验错误
  PARAM_VALIDE_ERROR(502),

  ///503 用户不存在
  USER_NOT_FOUND(503),

  /// 504 该账户不允许登录
  USER_NOT_AUTHO(504);

  ///
  final int code;
  const DioResponseCode(this.code);
  static DioResponseCode codeToStatus(int code) {
    return DioResponseCode.values.firstWhere((element) => element.code == code);
  }

  @override
  String toString() {
    return 'fetch_status_$code'.tr;
  }
}
